home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Full / NetObjects Fusion 9 Standard / NOF9_Full_EN.exe / data1.cab / FSI / lib / nof / ConfigFile.js < prev    next >
Encoding:
Text File  |  2005-11-16  |  10.5 KB  |  377 lines

  1. /****i* SOURCE_FILE/INFO
  2.     *
  3.     * NAME
  4.     *  ConfigFile.js
  5.     *
  6.     * USAGE
  7.     *  Part of Netobjects JavaScript Library.
  8.     *
  9.     * COPYRIGHT
  10.     *  Copyright ⌐ 2000-2005 Website Pros, Inc.
  11.     *  All Rights Reserved.
  12.     *
  13.     *  This is an unpublished work protected by Website Pros, Inc.
  14.     *  as a trade secret, and is not to be used or disclosed except as
  15.     *  expressly provided in a written license agreement executed by
  16.     *  you and Website Pros, Inc.
  17.     *
  18.     *      <copyright@websitepros.com>
  19.     *
  20.     * NOTES
  21.     *  JavaScript code.
  22.     *
  23.     *****/
  24. if (!IS.isModuleInitialized("IS.NOF.ConfigFile"))
  25. {
  26.     /****h* NOF_JavaScript_Library/NOF.ConfigFile
  27.     *
  28.     * NAME
  29.     *  NOF.ConfigFile
  30.     *
  31.     * DESCRIPTION
  32.     *  encapsulates generic profile settings. Base class for all profiles.
  33.     *
  34.     * External dependencies: NOF.XML.XmlDocument, NOF.Contract, NOF.UTIL.ArrayList
  35.     ****/
  36.     
  37.     /**
  38.     * Constructor    
  39.     * @param name - optional, the name of the configFile (profile)
  40.     */
  41.     function NOF_ConfigFile( name ) {
  42.         this.__proto__ = NOF_ConfigFile.prototype;
  43.         
  44.         if (arguments.length > 0){
  45.             this.xmlDoc         = NOF.XML.XmlDocument.create();
  46.             
  47.             this.configFileName = name;
  48.             if (arguments.length > 1)
  49.                 this.configModuleType = arguments[1];
  50.         }
  51.     }
  52.     
  53.     function NOF_ConfigFile_ProtoBuilder() {
  54.         var member = NOF_ConfigFile.prototype;
  55.         
  56.         member.fsiConfigFile    = null;
  57.         member.xmlDoc           = null;  
  58.         member.XMLTemplate      = null; 
  59.         member.configModuleType = null; 
  60.         member.fileExtension    = ".xml";
  61.         
  62.         member.invalidChars        = ["\\", "/", ":", "*", "?", "\"", "<", ">", "|"];
  63.         //default name space. Must pe overwrite in subclasses.
  64.         member.nameSpace = "xmlns:nof='http://www.netobjects.com/fusion/userProfile'";
  65.         
  66.         var method = NOF_ConfigFile.prototype;
  67.         
  68.         method.getConfigFilePath     = getConfigFilePath;
  69.         method.loadConfigFile         = loadConfigFile;
  70.         method.saveConfigFile         = saveConfigFile;
  71.         method.deleteConfigFile     = deleteConfigFile;
  72.         
  73.         method.getConfigFiles = getConfigFiles;
  74.         method.verifyDoc = verifyDoc;
  75.         
  76.         //dom handlers
  77.         method.getNodeValue = getNodeValue;
  78.         method.setNodeValue = setNodeValue;
  79.         method.getElementsByTagName = getElementsByTagName;
  80.         method.getChildNodes = getChildNodes;
  81.         method.getChildNodesValues = getChildNodesValues;
  82.         method.getNodeName = getNodeName;
  83.         method.GetFSIProfileObj = GetFSIProfileObj;
  84.         method.GetXMLDoc        = GetXMLDoc;
  85.         
  86.  
  87.         //
  88.         /**
  89.         **/
  90.         method.encrypt        = function (/*String*/ str) {
  91.             return this.GetFSIProfileObj().Encrypt(str);
  92.         }
  93.  
  94.         /**
  95.         **/
  96.         method.decrypt        = function (/*String*/ str) {
  97.             return this.GetFSIProfileObj().Decrypt(str);
  98.         }
  99.         
  100.         /**
  101.         *  Verifies if there is already an XML document loaded. If not, a template 
  102.         *  is loaded, to make possible the configFile saving.
  103.         */ 
  104.         function verifyDoc(){
  105.             var doc = this.GetXMLDoc();
  106.             if( doc.documentElement == null ){            
  107.                 doc.loadXML( this.XMLTemplate );
  108.             }
  109.             
  110.             NOF.Contract.Ensure(doc.documentElement != null, "Template parsing yield a null doc!");
  111.         }
  112.         
  113.         function GetXMLDoc()
  114.         {
  115.             if (this.xmlDoc == null){
  116.                 this.xmlDoc = NOF.XML.XmlDocument.create();
  117.             }
  118.             
  119.             return this.xmlDoc;
  120.         }
  121.         
  122.         function GetFSIProfileObj()
  123.         {
  124.             //TODO : bellow lines should be wrapped by a if clause. Not all Fusion
  125.             // version contains those dll's - problem occurs on /nof7 server app.
  126.             if (this.fsiConfigFile == null){
  127.                 this.fsiConfigFile  = new ActiveXObject(NOF.ProgId.FSIProfile);          
  128.             }
  129.             
  130.             return this.fsiConfigFile;
  131.         }
  132.         /**
  133.         *  Construct a list with the names of all the existing configFiles, for a 
  134.         *  specified type 
  135.         *  @param configModuleType - represents the configFile type: PhotoGallery,
  136.         *                       E-commerce, OutputSettings
  137.         *  @return configFiles list
  138.         */    
  139.         function getConfigFiles() {    
  140.             
  141.             var configFiles = new Array();
  142.             var profilePath = this.GetFSIProfileObj().GetProfilePath();
  143.             
  144.             var dirPath = profilePath +"/"+ this.configModuleType;
  145.             
  146.             var iter = new ActiveXObject(NOF.ProgId.FSIDirIterator);        
  147.             iter.DefineSearch(dirPath, '*'+this.fileExtension);
  148.             var q;
  149.             for (;;) {
  150.                 var p = iter.GetNext();
  151.                 if (!p.length)
  152.                     break;
  153.                 q = p.split('\\');
  154.                 p = q[q.length - 1];
  155.                 p = p.substring(0, p.lastIndexOf("."));
  156.                 configFiles[configFiles.length] = p;
  157.             }        
  158.             //alert("getConfigFiles \n "+configFiles.join("\n") );
  159.             return configFiles;    
  160.         }
  161.         
  162.         /**
  163.         * @return the full configFile path 
  164.         */    
  165.         function getConfigFilePath(){
  166.             
  167.             return ( this.GetFSIProfileObj().GetProfilePath() + "/" + this.configModuleType + "/" + this.configFileName + this.fileExtension );
  168.             
  169.         }
  170.         
  171.         /**
  172.         *  load a specified configFile, via FSIConfigFile object 
  173.         *  @param configFileName - the name of the configFile to be loaded, 
  174.         *                       if null, the name previously set for this configFile 
  175.         *                       is used.
  176.         *  @return true if succeeded, otherwise return false   
  177.         */
  178.         function loadConfigFile( configFileName ) {
  179.             
  180.             if ( configFileName ) {
  181.                 this.configFileName = configFileName;
  182.             }    
  183.             
  184.             var configFileAsString = this.GetFSIProfileObj().LoadFile( this.configModuleType + "/" + this.configFileName + this.fileExtension );        
  185.             
  186.             
  187.             try {
  188.                 
  189.                 this.xmlDoc = NOF.XML.XmlDocument.create(); 
  190.                 this.xmlDoc.loadXML( configFileAsString );
  191.                 
  192.                 return true;
  193.             }
  194.             catch(e) {
  195.                 //alert(e);
  196.                 return false;
  197.             }
  198.         }
  199.         
  200.         /**
  201.         * save the configFile coresponding to this object, based on its type, name    
  202.         * @param name - configFile name to be saved
  203.         * @return true if succesfull
  204.         */
  205.         function saveConfigFile(name) {
  206.             
  207.             if(name){
  208.                 this.configFileName = name;
  209.             }
  210.             
  211.             for ( var i = 0; i < this.invalidChars.length; i++ ) {
  212.                 if ( this.configFileName.indexOf(this.invalidChars[i]) > -1 ) {    
  213.                     return false;
  214.                 }    
  215.             }
  216.             
  217.             return this.GetFSIProfileObj().SaveFile( this.configModuleType + "/" + this.configFileName + this.fileExtension, this.xmlDoc.xml );
  218.         }
  219.         
  220.         /**
  221.         * delete the configFile coresponding to this object, based on its type, name    
  222.         * @param name - the configFile name to be deleted
  223.         * @return true if succesfull
  224.         */
  225.         function deleteConfigFile(name) {
  226.             if(name){
  227.                 this.configFileName = name;
  228.             }        
  229.             return this.GetFSIProfileObj().DeleteFile( this.configModuleType + "/" + this.configFileName + this.fileExtension );
  230.         }
  231.         
  232.         method.setCDATANodeValue = function (/*string*/ nodeName, /*string*/ value) {
  233.             //alert("setCDATANodeValue " + nodeName + " : " + value);
  234.             
  235.             if (value == null)
  236.                 return;
  237.             this.verifyDoc();
  238.             
  239.             if ( typeof(nodeName) == "string") {
  240.                 
  241.                 this.xmlDoc.setProperty("SelectionNamespaces", this.nameSpace);
  242.                 var node = this.xmlDoc.documentElement.selectSingleNode(nodeName);
  243.                 var CDATASection = this.xmlDoc.createCDATASection(value);
  244.                 //var children = node.childNodes;
  245.                 node.replaceChild(CDATASection, node.childNodes.item(0));
  246.                 //node.appendChild(CDATASection);
  247.                 
  248.             } else {
  249.                 nodeName.text = value;
  250.             }
  251.         }
  252.         
  253.         /**
  254.         * set a node value    
  255.         * @param nodeName - node qualified name or node reference
  256.         * @param value - new value    
  257.         */
  258.         function setNodeValue(nodeName, value) {
  259.             if (value == null)
  260.                 return;
  261.             this.verifyDoc();
  262.             
  263.             if ( typeof(nodeName) == "string") {
  264.                 this.xmlDoc.setProperty("SelectionNamespaces", this.nameSpace);
  265.                 var node = this.xmlDoc.documentElement.selectSingleNode(nodeName);
  266.                 node.text = value;
  267.             } else {
  268.                 nodeName.text = value;
  269.             }
  270.         }
  271.         
  272.         /**
  273.         * get a node value    
  274.         * @param nodeName - node qualified name or node reference
  275.         * @return node text
  276.         */
  277.         function getNodeValue(nodeName) {        
  278.             this.verifyDoc();
  279.             this.xmlDoc.setProperty("SelectionNamespaces", this.nameSpace);        
  280.             if ( typeof(nodeName) == "string") {            
  281.                 var node = this.xmlDoc.documentElement.selectSingleNode(nodeName);            
  282.                 return node.text;
  283.             } else
  284.             return nodeName.text;
  285.         }
  286.         
  287.         /**
  288.         * get a node qualified name    
  289.         * @param node - node qualified name or node reference
  290.         * @return node name
  291.         */
  292.         function getNodeName(node) {        
  293.             this.verifyDoc();
  294.             this.xmlDoc.setProperty("SelectionNamespaces", this.nameSpace);
  295.             if ( typeof(node) == "string") {
  296.                 var nodeRef = this.xmlDoc.documentElement.selectSingleNode(node);
  297.                 return nodeRef.nodeName;
  298.             } else
  299.             return node.nodeName;
  300.         }
  301.         
  302.         /**
  303.         * get a node list with the same qualified name
  304.         * @param tagName - node qualified name
  305.         * @return nodes list
  306.         */    
  307.         function getElementsByTagName(tagName) {
  308.             if (tagName == null || tagName == "")
  309.                 return null;
  310.             this.verifyDoc();
  311.             this.xmlDoc.setProperty("SelectionNamespaces", this.nameSpace);
  312.             return this.xmlDoc.documentElement.getElementsByTagName(tagName);
  313.         }
  314.         
  315.         /**
  316.         * get a node childs
  317.         * @param node - DOM node element
  318.         * @return childs list
  319.         */            
  320.         function getChildNodes(node) {
  321.             if (node == null)
  322.                 return null;
  323.             this.verifyDoc();
  324.             this.xmlDoc.setProperty("SelectionNamespaces", this.nameSpace);
  325.             if (typeof(node) == "string")
  326.                 node = this.xmlDoc.documentElement.selectSingleNode(node);
  327.             return node.childNodes;        
  328.         }
  329.         
  330.         /**
  331.         * get child nodes values from a specified node - parent
  332.         * @param node - node qualified name - parent node
  333.         * @return in Array with nodes values
  334.         */    
  335.         function getChildNodesValues(node) {
  336.             if (node == null)
  337.                 return null;
  338.             this.verifyDoc();
  339.             this.xmlDoc.setProperty("SelectionNamespaces", this.nameSpace);
  340.             if (typeof(node) == "string")
  341.                 node = this.xmlDoc.documentElement.selectSingleNode(node);
  342.             var childNodes     = this.getChildNodes(node);
  343.             var currentNode = childNodes.nextNode;
  344.             var list = new NOF.UTIL.ArrayList();
  345.             var i      = 0;
  346.             while (currentNode) {                        
  347.                 list.add(this.getNodeName(currentNode), this.getNodeValue(currentNode));
  348.                 i++;
  349.                 currentNode = childNodes.nextNode;            
  350.             }        
  351.             return list;        
  352.         }
  353.         
  354.         /** 
  355.         * @description get node attributes
  356.         * @param node : node reference or qualified name
  357.         */
  358.         function getNodeAttributes(node) {
  359.             this.verifyDoc();
  360.             this.xmlDoc.setProperty("SelectionNamespaces", this.nameSpace);
  361.             var nodeRef = null;
  362.             if ( typeof(node) == "string")
  363.                 nodeRef = this.xmlDoc.documentElement.selectSingleNode(node);            
  364.             else
  365.                 nodeRef = node;
  366.             
  367.             return nodeRef.attributes;
  368.         }
  369.         
  370.         
  371.         return this;
  372.     }    
  373.     
  374.     NOF_ConfigFile_ProtoBuilder();    
  375.     NOF.__proto__.ConfigFile = NOF_ConfigFile;
  376.     
  377. }